package com.itheima.controller.interceptor;


import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//添加到ioc容器中
@Component
public class ProjectInterceptor implements HandlerInterceptor {

    /*
        在controller方法之前执行: 前置通知
        1. 方法返回值:
            return true; //后续的controller,postHandle,afterCompletion方法才会执行
        2. 参数
            1). request : 请求
            2). response : 响应
            3). handler : 当前拦截器拦截的控制器方法
     */
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        System.out.println("preHandle");
//        System.out.println(handler);
        return true;
    }
    //在controller方法之后执行 : 后置通知
    //几乎不用
    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response,
                           Object handler,
                           ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }
    //在postHandler之后执行: afterCompletion(视图装配完毕之后执行)
    //几乎不用
    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}
